package myDoubleLinkedList;
class ListNode{
    public int val;
    public ListNode next;
    public ListNode prev;
    public ListNode(int val){
        this.val = val;
    }
}
public class MyDoubleLinkedList {

    public ListNode head;//指向双向链表的头节点
    public ListNode last;//指向双向链表的尾巴节点

    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size(){
        ListNode cur = this.head;
        int sum = 0;
        while(cur != null){
            sum++;
            cur = cur.next;
        }
        return sum;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

   //头插法
   public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (this.head == null){
            this.head = node;
            this.last = node;
        }else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
   }

   //尾插法
   public void addLast(int data){
        ListNode node = new ListNode(data);
        if (this.head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            node.prev = last;
            last = node;
        }
   }

   //删除第一次出现关键字为key的节点
   public void remove(int key){
        if (this.head == null){
            System.out.println("链表为空,删什么呢!");
            return;
        }
        ListNode cur = this.head;
        while(cur != null){
            if (cur.val == key){
                if (cur == this.head){
                    head = head.next;
                    if (head != null){
                        head.prev = null; //head不为空,意为链表不止一个元素.
                    }else{                //如果只有一个元素的时候,删除head,head往后走为空了,此时last也要置空.
                        last = null;
                    }
                }else if(cur == this.last){
                    last = last.prev;
                    last.next = null;
                }else{
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            }
                cur = cur.next;
        }
   }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (this.head == null){
            System.out.println("链表为空,删什么呢!");
            return;
        }
        ListNode cur = this.head;
        while(cur != null){
            if (cur.val == key){
                if (cur == this.head){
                    head = head.next;
                    if (head != null){
                        head.prev = null; //head不为空,意为链表不止一个元素.
                    }else{                //如果只有一个元素的时候,head为空,此时last也要置空.
                        last = null;
                    }
                }else if(cur == this.last){
                    last = last.prev;
                    if (last != null){
                        last.next = null;
                    }
                }else{
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }

    public ListNode searchIndex(int index){
        ListNode cur = this.head;
        for (int i = 0; i < index ; i++){
            cur = cur.next;
        }
        return cur;
    }

   //任意位置插入,第一个数据节点为0号下标
   public void addIndex(int index,int data){
       if (index < 0 || index > size()){
           System.out.println("插入的位置不合法!");
           return;
       }
       ListNode cur = searchIndex(index);
       ListNode node = new ListNode(data);
       if (index == 0){
           addFirst(data);
       }else if(index == size()){
           addLast(data);
       }else{
           cur.prev.next = node;
           node.prev = cur.prev;
           node.next = cur;
           cur.prev = node;
       }
   }

   public void clear(){
//        this.head = null;
//        this.last = null;
// head 和 last 都置空是比较粗暴的做法,虽然可以起到整个链表都被回收的作用,但是不建议用.
// 下面用更温柔的方法回收清空整个链表
       ListNode cur = this.head;
       while(cur != null){
           ListNode curNext = cur.next;
           cur.prev = null;
           cur.next = null;
           cur = curNext;
       }
       this.head = null;
       this.last = null;
       //虽然挨个节点的前驱和后继都置空了,但是头尾节点仍然在引用,因此需手动额外置空.
   }

}
